import Item from "antd/lib/list/Item";
import { QueryKey, useQueryClient } from "react-query";
import { Task } from "types/task";
import { reorder } from "./reorder";

export const useConfig = (queryKey: QueryKey, 
  callback: (target: any, old?: any[]) => any[]
  ) => {
    const queryClient = useQueryClient()
    return {
      onSuccess: () => queryClient.invalidateQueries(queryKey),
      async onMutate(target: any) {
        
        const previousItems = queryClient.getQueriesData(queryKey)
        queryClient.setQueriesData(queryKey, (old?: any[]) => {
          return callback(target, old)
          // return old?.map(project => project.id === target.id ? {...project, ...target} : project)
          // || []
        })
        return {previousItems}
      },
      onError(error: any, newItem: any, context: any) {
        queryClient.setQueriesData(queryKey, context.previousItems)
      }
    }


}

export const useDeleteConfig = (queryKey: QueryKey) => useConfig(queryKey, (target, old) => {
  return old?.filter(item => item.id !== target.id) || []
})

export const useEditConfig = (queryKey: QueryKey) => useConfig(queryKey, (target, old) => {
  
  return old?.map(item => item.id === target.id ? {...item, ...target} : item) || []
})

export const useAddConfig = (queryKey: QueryKey) => useConfig(queryKey, (target, old) => {
  return old ? [...old, target] : []
})


export const useReorderKanbanConfig = (queryKey: QueryKey) =>
  useConfig(queryKey, (target, old) => reorder({list: old, ...target}));

  export const useReorderTaskConfig = (queryKey: QueryKey) =>
  useConfig(queryKey, (target, old) => {
    const orderedList = reorder({ list: old, ...target }) as Task[];
    return orderedList.map((item) =>
      item.id === target.fromId
        ? { ...item, kanbanId: target.toKanbanId }
        : item
    );
  });